package com.chen.admin.controller;


import com.chen.admin.service.UserService;
import com.chen.admin.common.Result;
import com.chen.admin.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 用户列表展示
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping("/getUserPage")
   public PageInfo<User>  getUserPAge(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.getUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    /**
     * 新增用户信息
     */

    @PostMapping("/addUser")
    public Result addUSer(@RequestBody User user){
        Result result = userService.addUser(user);
        return result;
    }

    /**
     * 根据id进行删除用户操作
     *
     */
    @PostMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Integer id){
        Result result = userService.deleteUserById(id);
        return result;
    }

    /**
     * 根据用户名进行搜索
     */
    @GetMapping("/getUseByName")
    public Result getUserByName(@RequestParam String userName){
        User userInfo = userService.getUserByName(userName);
        return Result.success(userInfo);
    }

    /**
     * 根据id编辑用户信息
     */

    @PostMapping("/editUserInfo/{id}")
    public Result editUserInfo(@RequestBody User user,@PathVariable Integer id){
        Result result = userService.editUserInfo(user,id);
        return result;
    }

}
